package com.demo.function.jdk.lambda;

import com.alibaba.fastjson.JSON;
import com.demo.model.dto.OrderHeader;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author K
 */
@Slf4j
public class LambdaMap {

    /**
     * List转map，value是List对象
     */
    public void groupBy() {
        List<OrderHeader> list = new ArrayList<>();

        OrderHeader orderHeader = new OrderHeader();
        orderHeader.setOrderSn("aa");
        list.add(orderHeader);

        orderHeader = new OrderHeader();
        orderHeader.setOrderSn("aa");
        list.add(orderHeader);

        orderHeader = new OrderHeader();
        orderHeader.setOrderSn("bb");
        list.add(orderHeader);

        Map<String, List<OrderHeader>> map = list.stream().collect(Collectors.groupingBy(OrderHeader::getOrderSn));
        log.info("Collectors.groupingBy：{}", JSON.toJSONString(map));
    }
}
